package com.dmai.capacity.platform.user.service.impl;

import com.dmai.capacity.platform.common.constant.CodeEnum;
import com.dmai.capacity.platform.common.constant.CommonConstant;
import com.dmai.capacity.platform.common.dto.PageResult;
import com.dmai.capacity.platform.common.dto.PageResultData;
import com.dmai.capacity.platform.common.model.LoginAppUser;
import com.dmai.capacity.platform.oauth.common.util.AuthUtils;
import com.dmai.capacity.platform.user.mapper.SysMenuButtonDao;
import com.dmai.capacity.platform.user.mapper.SysMenuDao;
import com.dmai.capacity.platform.user.model.SysMenu;
import com.dmai.capacity.platform.user.model.SysMenuButton;
import com.dmai.capacity.platform.user.model.SysMenuPermission;
import com.dmai.capacity.platform.user.model.SysOrganization;
import com.dmai.capacity.platform.user.service.ISysMenuPermissionService;
import com.dmai.capacity.platform.user.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;


@Service
public class SysMenuServiceImpl implements ISysMenuService {

    @Autowired
    private SysMenuDao menuDao;

    @Autowired
    private SysMenuButtonDao buttonDao;

    @Autowired
    private ISysMenuPermissionService menuPermissionService;

    /**
     * 添加
     *
     * @param sysMenu
     */
    public int save(SysMenu sysMenu) {
        return menuDao.save(sysMenu);
    }

    /**
     * 修改
     *
     * @param sysMenu
     */
    public int update(SysMenu sysMenu) {
        return menuDao.update(sysMenu);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        SysMenu orig = menuDao.findById(id);
        if (orig == null) {
            throw new IllegalArgumentException("找不到对应的数据");
        }
        return menuDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SysMenu findById(String id) {
        SysMenu menu = menuDao.findById(id);
        if (menu == null) {
            throw new IllegalArgumentException("找不到对应的数据");
        }
        List<SysMenuButton> buttons = buttonDao.findByMenu(id);
        menu.setButtons(buttons);

        return menu;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SysMenu> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT) != null && MapUtils.getInteger(params, CommonConstant.PAGE_SIZE) != null)
            PageHelper.startPage(MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT), MapUtils.getInteger(params, CommonConstant.PAGE_SIZE), true);

        List<SysMenu> list = menuDao.findAll(params);
        PageInfo<SysMenu> pageInfo = new PageInfo(list);


        return PageResult.<SysMenu>builder().data(PageResultData.<SysMenu>builder().records(pageInfo.getList()).total(pageInfo.getTotal()).pages(pageInfo.getPages()).build()).code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SysMenu> findAll(Map<String, Object> params) {
        List<SysMenu> menus = menuDao.findAll(params);
        for (SysMenu menu : menus) {
            List<SysMenuButton> buttons = buttonDao.findByMenu(menu.getId());
            menu.setButtons(buttons);
        }
        return menus;
    }

    @Override
    public List<SysMenu> findPermission(Map<String, Object> params) {
        LoginAppUser loginAppUser = AuthUtils.getLoginAppUser();
        List<SysMenu> menus = findAll(params);
        List<SysMenuPermission> permissions = menuPermissionService.findByUser(loginAppUser);

        menus = menus.stream().filter(t -> permissions.stream().filter(p -> "menu".equalsIgnoreCase(p.getItemType())).map(SysMenuPermission::getItemId).collect(Collectors.toList()).contains(t.getId())).collect(Collectors.toList());

        for (SysMenu menu : menus) {
            menu.setButtons(menu.getButtons().stream().filter(t -> permissions.stream().filter(p -> "button".equalsIgnoreCase(p.getItemType())).map(p -> Integer.parseInt(p.getItemId())).collect(Collectors.toList()).contains(t.getId())).collect(Collectors.toList()));
        }

        return menus;
    }
}
